package cn.valorin.event;

import static cn.valorin.configuration.languagefile.MessageSender.sm;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import cn.valorin.Main;
import cn.valorin.data.Data;
import cn.valorin.item.Bean;
import cn.valorin.item.BeanSeed;
import cn.valorin.item.RawBean;
import cn.valorin.item.RiceSeed;
import cn.valorin.util.CheckLore;
import cn.valorin.via.Via;

public class PlantEvent implements Listener {
	@SuppressWarnings("deprecation")
	@EventHandler(priority=EventPriority.HIGHEST)
	public void plant1(PlayerInteractEvent e) { //种植豆子
		if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { return; }
		Block block = e.getClickedBlock();
		Player player = e.getPlayer();
		if (e.isCancelled()) { return; }
		if (block.getType().equals(Via.soil)) {
			if (CheckLore.check(player.getItemInHand(), BeanSeed.get(player))) {
				Data.createPlant(
						block.getWorld().getName(),
						block.getX(),
						block.getY()+1,
						block.getZ(),
						0);
				e.setCancelled(true);
				block.getRelative(BlockFace.UP).setType(Via.crops);
				if (player.getItemInHand().getAmount() == 1) {
					player.setItemInHand(new ItemStack(Material.AIR));
				} else {
					ItemStack temp = player.getItemInHand();
					temp.setAmount(temp.getAmount() - 1);
					player.setItemInHand(temp);
				}
				Main.getInstance().addData();
				sm("&a你种下了豆子",player);
			}
		}
	}
	@SuppressWarnings("deprecation")
	@EventHandler(priority=EventPriority.HIGHEST)
	public void plant2(PlayerInteractEvent e) { //种植糯稻
		if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { return; }
		Block block = e.getClickedBlock();
		Player player = e.getPlayer();
		if (e.isCancelled()) { return; }
		if (block.getType().equals(Via.soil)) {
			if (CheckLore.check(player.getItemInHand(), RiceSeed.get(player))) {
				Data.createPlant(
						block.getWorld().getName(),
						block.getX(),
						block.getY()+1,
						block.getZ(),
						1);
				e.setCancelled(true);
				block.getRelative(BlockFace.UP).setType(Via.crops);
				if (player.getItemInHand().getAmount() == 1) {
					player.setItemInHand(new ItemStack(Material.AIR));
				} else {
					ItemStack temp = player.getItemInHand();
					temp.setAmount(temp.getAmount() - 1);
					player.setItemInHand(temp);
				}
				Main.getInstance().addData();
				sm("&a你种下了糯稻",player);
			}
		}
	}
	
	@SuppressWarnings("deprecation")
	@EventHandler
	public void antiPlant(PlayerInteractEvent e) { //防止种植道具
		Block block = e.getClickedBlock();
		Player player = e.getPlayer();
		if (e.isCancelled()) { return; }
		if (block.getType().equals(Via.soil)) {
			if (CheckLore.check(player.getItemInHand(), RawBean.get(player)) ||
				CheckLore.check(player.getItemInHand(), Bean.get(player))) {
				e.setCancelled(true);
			}
		}
	}
}
